//
//  BSNodeCocos2dx.cpp
//  Cocos2dXTest
//
//  Created by bean on 13/4/30.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#include "BSNodeCocos2dx.h"
#import "BSXMLPaser.h"
#import "BSSpriterData.h"
#import "BSAnimation.h"
#import "BSMainLine.h"
#import "BSTimeLine.h"
using namespace cocos2d;
BSNodeCocos2dx::BSNodeCocos2dx(void)
{
    
}

bool BSNodeCocos2dx::init(void)
{
    
    m_sprites = nil;
    m_race = "orc";
    m_shield = "shield";
    m_weapon = "sword";
    m_hat = "hat";
    return true;
}

BSNodeCocos2dx::~BSNodeCocos2dx(void)
{
//    delete m_hat;
//    delete m_race;
//    delete m_weapon;
//    delete m_shield;
    this->clearSprite();
    [(NSObject*)m_spriteData release];
}

void BSNodeCocos2dx::setSpriterData(const char* dataFn)
{
    BSSpriterData* spriteData = [BSXMLPaser spriteDataWithFile:
                                 [NSString stringWithUTF8String:dataFn]];
    [spriteData retain];
    m_spriteData = spriteData;
}
float BSNodeCocos2dx::playAnimation(const char *animationName, bool isRepeat, CCCallFunc* fun)
{
    this->clearSprite();
    BSSpriterData* spriterData = (BSSpriterData*)m_spriteData;
    BSAnimation* animation = [spriterData animationWithAnimationName:
                              [NSString stringWithUTF8String:animationName]];
    
    m_sprites = [spriterData spriteArrayWithAniamtion:animation
                                                   shield:[NSString stringWithUTF8String:m_shield]
                                                      hat:[NSString stringWithUTF8String:m_hat]
                                                   weapon:[NSString stringWithUTF8String:m_weapon]
                                                     race:[NSString stringWithUTF8String:m_race]];
    m_sprites->retain();
    

    for (int i = 0; i < m_sprites->count(); i++)
    {
        CCSprite* s = dynamic_cast<CCSprite*>(m_sprites->objectAtIndex(i));
        int z = [spriterData zOrderWithKeyId:0 timelineID:s->getTag()
                                  inMainLine:animation.mainline];
        this->addChild(s, z);
        [spriterData setAnimation:animation
                     toTimeLineId:s->getTag()
                           sprite:s
                         isRepeat:isRepeat];
    }
    if (fun && !isRepeat)
    {
        CCDelayTime* delayTime = CCDelayTime::create((float)[animation length] / 1000);
        CCFiniteTimeAction* seqAct = cocos2d::CCSequence::createWithTwoActions(delayTime, fun);
        this->runAction(seqAct);
    }
    return [animation length];
}

void BSNodeCocos2dx::clearSprite()
{
    if (m_sprites) {
        for (int i = 0; i < m_sprites->count(); i++)
        {
            
            CCSprite* s = dynamic_cast<CCSprite*>(m_sprites->objectAtIndex(i));
            s->stopAllActions();
            this->removeChild(s, YES);
        }
        m_sprites->autorelease();
        m_sprites = NULL;
    }
}
void BSNodeCocos2dx::stopActSprite()
{
    if (m_sprites) {
        for (int i = 0; i < m_sprites->count(); i++)
        {
            
            CCSprite* s = dynamic_cast<CCSprite*>(m_sprites->objectAtIndex(i));
            s->stopAllActions();
        }
    }
}
void BSNodeCocos2dx::setWeapon(const char *weapon)
{
    m_weapon = weapon;
}
void BSNodeCocos2dx::setRece(const char *race)
{
    m_race = race;
}
void BSNodeCocos2dx::setShield(const char *shield)
{
    m_shield = shield;
}
void BSNodeCocos2dx::setHat(const char *hat)
{
    m_hat = hat;
}